import { defineComponent } from 'vue'

const AuthController = defineComponent({
  name: 'AuthController',
  props: {
    requirePermission: {
      type: String,
      required: true,
    },
  },
  setup(props, { slots }) {
    const userStore = useUserStore()
    const permissions = userStore.userInfo?.permissions || []
    const hasPermission = permissions.includes(props.requirePermission)
    return () => {
      if (hasPermission) {
        return slots.default ? slots.default() : null
      } else {
        return slots.noAccess ? slots.noAccess() : null
      }
    }
  },
})

export default AuthController
